import http from 'axios'
import * as error from './error.js'
const baseURL = url.baseUrl
import JSONbig from 'json-bigint';
const instance = http.create({
  baseURL,
  timeout: 15000,
  transformResponse: [
    function(data) {
      if (typeof data === 'string') {
        const JSONbigString = new JSONbig({
          storeAsString: true
        });
        return JSONbigString.parse(data);
      } else {
        return data;
      }
    }
  ]
});
// request interceptors
instance.interceptors.request.use((config, promise) => {
  // config.body = qs.stringify(config.body,{ indices: false });

  // let check = whiteList.findIndex(item=>{return item === config.url})

  // config.headers.Authorization = token;
  return config
  // if(check === -1){
  // 	if(token != ''){
  // 		config.headers['X-ACCESS-TOKEN'] = token;
  // 		return config
  // 	}
  // }else{
  // 	return config
  // }
}, (error) => {
  return Promise.reject(error)
})

instance.interceptors.response.use(function(response) {
  if (response.status == 200) {
    return response.data.data;
  } else {
    return response
  }
}, (error) => {
  if (error.status === 500) {
    uni.showToast({
      title: '网络错误',
      icon: 'none'
    })
  }
});

export default instance
